package com.jming.yygh.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jming.yygh.enums.RefundStatusEnum;
import com.jming.yygh.model.order.PaymentInfo;
import com.jming.yygh.model.order.RefundInfo;
import com.jming.yygh.order.mapper.RefundInfoMapper;
import com.jming.yygh.order.service.RefundInfoService;

import java.util.Date;

/**
 * @author jming
 * @version 0.0.1
 * @date 2021/05/16
 */
public class RefundInfoServiceImpl
        extends ServiceImpl<RefundInfoMapper, RefundInfo>
        implements RefundInfoService {


    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {

        // 判断是否有重复数据添加
        QueryWrapper<RefundInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", paymentInfo.getOrderId());
        wrapper.eq("payment_type", paymentInfo.getPaymentType());
        RefundInfo refundInfo = baseMapper.selectOne(wrapper);

        // 有相同的数据
        if (null != refundInfo) {
            return refundInfo;
        }

        // 添加数据
        refundInfo = new RefundInfo();
        refundInfo.setCreateTime(new Date());
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        refundInfo.setSubject(paymentInfo.getSubject());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        baseMapper.insert(refundInfo);

        return refundInfo;

    }
}
